package main

import (
	"fmt"
	"mapreduce/common"
	"mapreduce/master"
	"strconv"
)

/**
  用户自定义的Map函数
*/
func Map(lineNum string, line string) []common.KeyValue {
	var keyValueSlc []common.KeyValue
	keyValue := common.KeyValue{
		Key:   line,
		Value: strconv.Itoa(1),
	}
	return append(keyValueSlc, keyValue)
}

/*
 用户自定义的Reduce函数
*/
func Reduce(key string, valueSlc []string) common.KeyValue {
	var count int
	for _, value := range valueSlc {
		num, _ := strconv.Atoi(value)
		count = count + num
	}
	var keyValue = common.KeyValue{
		Key:   key,
		Value: strconv.Itoa(count),
	}
	return keyValue
}

func main() {
	var job common.Job
	job.JobName = "testMap"
	var mapTaskFilePathSlc = make([]string, 1)
	mapTaskFilePathSlc[0] = "C:/Users/zhangyiyang/Desktop/mapreduce.txt"
	job.TaskFilePathSlc = mapTaskFilePathSlc
	job.MapNum = 2
	job.ReduceNum = 2
	job.MapFunc = Map
	job.ReduceFunc = Reduce
	fmt.Println("等待MapReduce执行。。。")
	master.SubmitJob(job)
}
